package com.yuexunit.mobilenurse.module.BloodManage.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.BloodManage.bean.CheckDataBean;
import com.yuexunit.mobilenurse.module.BloodManage.model.impl.ReceiveModel;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.IReceivePresenter;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.impl.ReceivePresenter;
import com.yuexunit.mobilenurse.module.BloodManage.ui.fragment.Frag_Blood_Cancle;
import com.yuexunit.mobilenurse.module.BloodManage.ui.fragment.Frag_Blood_Cancle_Lachsis;
import com.yuexunit.mobilenurse.module.BloodManage.ui.fragment.Frag_Blood_Receive;
import com.yuexunit.mobilenurse.module.BloodManage.ui.fragment.Frag_Blood_Record_Lachsis;
import com.yuexunit.mobilenurse.module.BloodManage.ui.fragment.Frag_Blood_Send_State_Lachsis;
import com.yuexunit.mobilenurse.module.BloodManage.ui.view.IReceiveView;
import com.yuexunit.mobilenurse.widget.LoadingDialog;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/21 0021.
 */

public class Act_Blood_Main_Lachsis extends KJActivity implements IReceiveView {
    @Bind(R.id.tv_1)
    TextView tv_1;
    @Bind(R.id.img_1)
    ImageView img_1;
    @Bind(R.id.tv_2)
    TextView tv_2;
    @Bind(R.id.img_2)
    ImageView img_2;
    @Bind(R.id.tv_3)
    TextView tv_3;
    @Bind(R.id.img_3)
    ImageView img_3;
    @Bind(R.id.tv_4)
    TextView tv_4;
    @Bind(R.id.img_4)
    ImageView img_4;

    KJFragment mContentFragment;
    KJFragment fragment_1;
    KJFragment fragment_2;
    KJFragment fragment_3;
    KJFragment fragment_4;

    LoadingDialog dialog;
    IReceivePresenter presenter;

    private ValueBroadcastReceiver valueBroadcastReceiver = null;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_blood_main);
        ButterKnife.bind(this);
    }


    @Override
    public void initData() {
        super.initData();
        dialog = new LoadingDialog(this);
        presenter = new ReceivePresenter(new ReceiveModel(), this);
        fragment_1 = new Frag_Blood_Receive();
        fragment_2 = new Frag_Blood_Send_State_Lachsis();
        fragment_3 = new Frag_Blood_Record_Lachsis();
        fragment_4 = new Frag_Blood_Cancle_Lachsis();

        mContentFragment = fragment_1;
        changeFragment(R.id.framelayout_content, mContentFragment);
    }

    @OnClick({R.id.back, R.id.rl_1, R.id.rl_2, R.id.rl_3, R.id.rl_4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.rl_1:
                tv_1.setTextColor(getResources().getColor(R.color.doc_advice_01));
                img_1.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                tv_2.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_2.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_3.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_3.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_4.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_4.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                mContentFragment = fragment_1;
                changeFragment(R.id.framelayout_content, mContentFragment);
                break;
            case R.id.rl_2:
                tv_1.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_1.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_2.setTextColor(getResources().getColor(R.color.doc_advice_01));
                img_2.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                tv_3.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_3.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_4.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_4.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                mContentFragment = fragment_2;
                changeFragment(R.id.framelayout_content, mContentFragment);
                break;
            case R.id.rl_3:
                tv_1.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_1.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_2.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_2.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_3.setTextColor(getResources().getColor(R.color.doc_advice_01));
                img_3.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                tv_4.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_4.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                mContentFragment = fragment_3;
                changeFragment(R.id.framelayout_content, mContentFragment);
                break;
            case R.id.rl_4:
                tv_1.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_1.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_2.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_2.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_3.setTextColor(getResources().getColor(R.color.doc_advice_03));
                img_3.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tv_4.setTextColor(getResources().getColor(R.color.doc_advice_01));
                img_4.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                mContentFragment = fragment_4;
                changeFragment(R.id.framelayout_content, mContentFragment);
                break;
        }
    }

    @Override
    public void onResume(){
        super.onResume();
        startBarcodeBroadcastReceiver();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onStop(){
        super.onStop();
        stopBarcodeBroadcastReceiver();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        stopBarcodeBroadcastReceiver();
    }

    /**
     * 停止接收广播
     */
    private void stopBarcodeBroadcastReceiver() {
        try {
            if (valueBroadcastReceiver != null)
                unregisterReceiver(valueBroadcastReceiver);
        } catch (Exception e) {

        }
    }

    /**
     * 开始接收广播
     */
    private void startBarcodeBroadcastReceiver() {
        try {
            if (valueBroadcastReceiver == null)
                valueBroadcastReceiver = new ValueBroadcastReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction("lachesis_barcode_value_notice_broadcast");
            registerReceiver(valueBroadcastReceiver, filter);
        } catch (Exception e) {

        }
    }

    /**
     * 关机广播接收者
     *
     * @author
     *
     */
    private class ValueBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            Log.i("ValueBroadcastReceiver", "onReceive......");
            final String value = arg1
                    .getStringExtra("lachesis_barcode_value_notice_broadcast_data_string");
            if(!value.equals(""))
            {
                String code = "15"+value;
                if(code.length() == Integer.valueOf(PreferenceHelper.readString(aty, AppConfig.JCI_INFO, AppConfig.JCI_YX_RULE)))
//                    if(code.length()==10)
                {
                    Map<String, String> params = new HashMap<>();
                    params.put("bagNo", code);
                    params.put("hisJobNum", PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID));
                    params.put("jobName", PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGNAME));
                    params.put("receiveTime", StringUtils.getDataTime("yyyy-MM-dd HH:mm:ss"));
                    TelephonyManager telephonyManager = (TelephonyManager) aty
                            .getSystemService(Context.TELEPHONY_SERVICE);
                    params.put("setNo", telephonyManager.getDeviceId());
                    presenter.receiveBlood(params);
                }
            }
        }
    }


    @Override
    public void receiveBlood(CheckDataBean checkDataBean) {
        if (checkDataBean.getStatus().equals("200")) {
            ViewInject.toast("接血成功");
        } else {
            ViewInject.toast("接血失败");
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                dialog.dismiss();
                break;

        }
    }

}
